package com.dingding.gmall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.order.dao.OrderItemDao;
import com.dingding.gmall.order.entity.OrderEntity;
import com.dingding.gmall.order.entity.OrderItemEntity;
import com.dingding.gmall.order.entity.OrderReturnReasonEntity;
import com.dingding.gmall.order.service.OrderItemService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Service("orderItemService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @RabbitHandler
    public void receiveMsg(Message message, OrderReturnReasonEntity content, Channel channel){
        System.out.println("接受到消息..."+ content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 只要broker没有收到ack，那么消息的状态一直是unack，即使消费者宕机，消息会重新编程Ready状态，重启后会重发
     * @param message
     * @param content
     * @param channel
     */
    @RabbitHandler
    public void receiveMsg(Message message, OrderEntity content, Channel channel){
        System.out.println("接受到消息..."+ content);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            //签收，表示消息被消费者消费，在队列把这条消息删除
            channel.basicAck(deliveryTag, false);
            throw new RuntimeException();
        } catch (Exception e) {
            e.printStackTrace();
            try {
//                long deliveryTag, boolean multiple,boolean requeue
                //拒接签收让消息重回队列
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    @Override
    public List<OrderItemEntity> getOrderItemList(String orderSn) {
        return this.list(new QueryWrapper<OrderItemEntity>().eq("order_sn", orderSn));
    }

    @Override
    public List<OrderItemEntity> listOrderItemByOrderSn(List<String> collect) {
        return this.list(new QueryWrapper<OrderItemEntity>().in("order_sn",collect));
    }
}